package com.spa.infrastructure.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spa.application.command.admin.MenuAddCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.menu.geeker.MenuDTO;
import com.spa.application.dto.admin.menu.geeker.MenuSelectDTO;
import com.spa.domain.entity.Menu;
import com.spa.infrastructure.repository.po.MenuPO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 对象映射框架配置.
 *
 * @author liangjun
 */
@Mapper
public interface MenuMapStruct {

    static ObjectMapper mapper = new ObjectMapper();

    MenuMapStruct INSTANCE = Mappers.getMapper(MenuMapStruct.class);

    MenuPO addCommand2Po(MenuAddCommand command);

    List<MenuDTO> entity2MenuDTO4List(List<Menu> menuList);

    PageResult<MenuDTO> entity2MenuDTO4Page(Page<Menu> menuList);

    List<MenuSelectDTO> dto2dtoList(List<MenuDTO> menuDTOList);

    @Mapping(source = "id", target = "value")
    @Mapping(source = "meta.title", target = "label")
    MenuSelectDTO dto2dto(MenuDTO menuDTO);

    @Mapping(source = "meta", target = "meta", qualifiedByName = "str2Meta")
    MenuDTO entity2MenuDTO(Menu menu);

    List<Menu> po2Entity4List(List<MenuPO> poList);

    Page<Menu> po2Entity4Page(Page<MenuPO> poList);

//    @Named("meta2Str")
//    default String meta2Str(AddMenuCommand.Meta meta) throws JsonProcessingException {
//        return JsonUtil.obj2Str(meta);
//    }
//
    @Named("str2Meta")
    default MenuDTO.Meta str2Meta(String meta) throws JsonProcessingException {
        return mapper.readValue(meta, MenuDTO.Meta.class);
    }

}
